from PlaylistHelperUtil import PlaylistHelper
import Common
from M3u8ReaderUtil import M3u8Reader
from ADbaseUtil import ADbase

class MovieDB:
    
    def __init__(self, logfile):
        self.logger = logfile
        self.cutoffFileList = []    # TODO: temporarily only
        self.PopulateCutOffList()
    
    def GetItems(self, m3u8Req, lastRequest, N = 5):
        '''
        if lastRequest is None: First time user
        figure out what items to send based on (name) channel;
        incoming request for a m3u8
        '''
        firstTime = False
        
        if lastRequest == None: #first time user
            self.logger.logthis("First time user: ", Common.Logger.GET_Request)
            m3u8Name = Common.GetLatest(m3u8Req)
            self.logger.logthis("m3u8 found: " + str(m3u8Name), Common.Logger.GET_Request)
            tempM3u8 = M3u8Reader(m3u8Name).getM3u8()
            lastRequest = tempM3u8.GetFirstTS()
            self.logger.logthis("Last request found: " + str(lastRequest), Common.Logger.GET_Request)
            lastRequest = Common.NextAvailableRequest(lastRequest)
            self.logger.logthis("Last request set to: " + str(lastRequest), Common.Logger.GET_Request)
            firstTime = True
            self.logger.logthis("First ----- Time", Common.Logger.GET_Request)
            
        
        splitReq = lastRequest.split("-")
        prefix = splitReq[0]
        ch = splitReq[1]
        base = splitReq[2].rstrip(".ts")
        base = int(base)
        if not firstTime:
            base = base + 1
        
        seq = 0
            
        adInserted = False
        playlist = PlaylistHelper(self.logger)
        
        playlist.Begin(lastRequest)
        
        #assuming channel to be 1 always
        for i in range(N):
            seq = int(base) + i 
            fileName = prefix+"-"+ch+"-"+str(seq) + ".ts"
            
            if self.isAdTriggerFile(fileName):
                adInserted = True
                userAdFile = "ad-android-01.m3u8"
                
                if Common.GetUser() == "enrique":
                    userAdFile = "ad-lexus-01.m3u8"
                adM3u8 = M3u8Reader(userAdFile).getM3u8()
                playlist.Push_Back(adM3u8, PlaylistHelper.M3U8_TYPE)
            
            playlist.Push_Back(fileName, PlaylistHelper.TS_TYPE)
        
        playlist.End()
        
        if Common.DEBUG and adInserted:
            playlist.Dump(str(base) + ".dump")
            
        return playlist
    
    def isAdTriggerFile(self, mov):
        found = False
        for item in self.cutoffFileList:
            if Common.strContains(mov, item):
                found = True
                break
        
        if found:
            self.logger.logthis("Ad cut off found at: " + mov, Common.Logger.GET_Request)
        
        return found
    
    def PopulateCutOffList(self):
        adDB = ADbase(self.logger)
        self.cutoffFileList = adDB.GetAdList()
